//
//  SPMineViewController.m
//  Sport
//
//  Created by Cass on 2017/12/2.
//  Copyright © 2017年 Cass. All rights reserved.
//

#import "SPMineViewController.h"
#import "SPMyInformationViewController.h"
#import "SPStateViewController.h"
#import "SPOrderViewController.h"
#import "SPCollectViewController.h"
#import "SPIdeaViewController.h"
#import "SPSetupViewController.h"
#import "SPEditViewController.h"
#import "SPFriendCell.h"
#import "SPLoginViewController.h"
@interface SPMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) UIButton *noticeButton;
//@property (weak, nonatomic) UIButton *stateButton;
@property (weak, nonatomic) UIButton *fansBUtton;
@property (nonatomic, copy) NSString *headImage;
@property (nonatomic, copy) NSString *nickName;
@end

@implementation SPMineViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    self.navigationItem.title = @"我的";
    self.headImage = [userDefault objectForKey:@"headimage"];
    self.nickName = [userDefault objectForKey:@"nickname"];
}
//- (UIButton *)noticeButton{
//    if (!_noticeButton) {
//        _noticeButton = [[UIButton alloc]init];
//        [_noticeButton addTarget:self action:@selector(toState) forControlEvents:UIControlEventTouchUpInside];
//    }
//    return _noticeButton;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    
}

//- (void)toState{
//    SPStateViewController *stateVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPStateViewController class])];
//    stateVC.title = @"我的动态";
//    [self.navigationController pushViewController:stateVC animated:YES];
//}
#pragma mark - UITableViewDelegate -
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"You Selected %@",[tableView cellForRowAtIndexPath:indexPath].textLabel.text);
    if (indexPath.section == 0) {
        SPEditViewController *editVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPEditViewController class])];
        editVC.title = @"编辑资料";
        [self.navigationController pushViewController:editVC animated:YES];
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            SPCollectViewController *collectVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPCollectViewController class])];
            collectVC.title = @"收藏";
            [self.navigationController pushViewController:collectVC animated:YES];
        }
        else if (indexPath.row == 1) {
            SPIdeaViewController *ideaVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPIdeaViewController class])];
            ideaVC.title = @"意见反馈";
            [self.navigationController pushViewController:ideaVC animated:YES];
        }else if (indexPath.row == 2){
            SPSetupViewController *setupVC = [[UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPSetupViewController class])];
            setupVC.title = @"设置";
            [self.navigationController pushViewController:setupVC animated:YES];
        }

    }
        else if (indexPath.section == 2){
            [self removeUserInfo];
            [SPHUDUtil showHUDWithDismissTimeInterval:1 message:@"退出成功" hudType:SPHUDTypeSuccess];
            
            dispatch_time_t timer = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
            dispatch_after(timer, dispatch_get_main_queue(), ^{
                SPLoginViewController *loginVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPLoginViewController class])];
                [self presentViewController:loginVC animated:YES completion:nil];
//            [self.navigationController pushViewController:loginVC animated:YES];
            });

        }
    
}

- (void)removeUserInfo{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault removeObjectForKey:@"phoneNumber"];
    [userDefault removeObjectForKey:@"password"];
    [userDefault removeObjectForKey:@"isLogin"];
    [userDefault removeObjectForKey:@"nickname"];
    [userDefault removeObjectForKey:@"ID"];
    [userDefault removeObjectForKey:@"age"];
    [userDefault removeObjectForKey:@"accountName"];
    [userDefault removeObjectForKey:@"gender"];
    [userDefault removeObjectForKey:@"hobby"];
    [userDefault removeObjectForKey:@"headimage"];
    [userDefault removeObjectForKey:@"occupation"];
    [userDefault removeObjectForKey:@"persignature"];
    [userDefault removeObjectForKey:@"location"];
    [userDefault synchronize];

}
#pragma mark - UITableViewDataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0) {
        return 1;
    }else if (section == 1){
        return 3;
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = self.nickName;
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:self.headImage] placeholderImage:[UIImage imageNamed:@"ha"]];
    }else if (indexPath.section == 1){
//        SPFriendCell *friendCell = [SPFriendCell cellWithTableView:tableView];
//        friendCell.stateBtn = self.stateButton;
//        self.noticeButton = friendCell.noticeBtn;
//        self.fansBUtton = friendCell.noticeBtn;
//        cell = friendCell;
//        cell.textLabel.text = @"我的动态";
        if (indexPath.row ==0) {
            cell.textLabel.text = @"收藏";
        }
        else if (indexPath.row == 1) {
            cell.textLabel.text = @"意见反馈";
        }else if (indexPath.row == 2){
            cell.textLabel.text = @"设置";
        }
    }
    else if(indexPath.section == 2){
            cell.textLabel.text = @"退出";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 87.0f;
    }else{
        return 57.0f;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
